<?php 
class CategoryModel extends Model{
	public $table='category';

	//执行create函数时自动验证
	public $validate=array(
		array('cat_name','nonull','栏目名称不能为空',2,3)
	);

	//栏目添加模型
	public function addCategory(){
		if($this->create()){
			if($this->add()){
				$this->updateCache();
				return true;
			}else{
				$this->error='栏目添加失败';
				return false;
			}
		}
	}

	//栏目编辑
	public function editCategory(){
		if($this->create()){
			if($this->save()){
				//编辑成功时更新缓存
				$this->updateCache();
				return true;
			}else{
				$this->error='栏目编辑失败';
			}
		}
	}

	//栏目删除
	public function delCategory(){
		$cid=Q('cid',0,'intval');		
			if($this->del($cid)){
				//删除时更新缓存
				$this->updateCache();
				return true;
			}else{
				$this->error='删除失败';
				return false;
			}	
	}

	//更新缓存
	public function updateCache(){
		$data=M('category')->all();
		$cache=array();
		if($data){
			//显示树状目录结构
			$data=Data::tree($data,'cat_name');
			foreach ($data as $d) {
				$cache[$d['cid']]=$d;
			}
		}
		if(S('category',$cache)){
			return true;
		}else{
			$this->error='缓存更新失败';
		}
		
	}


}

?>